import { Component, Input, OnInit } from '@angular/core';
import { ModalController, NavParams } from '@ionic/angular';
declare let $: any;
@Component({
  selector: 'app-modal-multiple-select3-auto-full',
  templateUrl: './modal-multiple-select3-auto-full.component.html',
  styleUrls: ['./modal-multiple-select3-auto-full.component.scss'],
})
export class ModalMultipleSelect3AutoFullComponent implements OnInit {

  @Input() data: any;
  showCheckAll = true;
  constructor(
    public modalController: ModalController,
    public navParams: NavParams,
  ) { }
  ngOnInit() {
    this.data  =  JSON.parse(JSON.stringify(this.navParams.data.value));
    // 初始值
    let lastValue: any;
    if (this.data.value && typeof(this.data.value).isArray  ) {
      lastValue = this.data.value;

    } else if (this.data.value && typeof(this.data.value).isString) {
      lastValue = this.data.value ? this.data.value.split(',') : [];
    } else {
      lastValue = [];
    }
    this.data.value = lastValue;
    const hasOpen = false; // 默认打开第一个有值类
    if (lastValue.length > 0) {
      this.data.children.forEach(element => {

        if (element.children && element.children.length > 0 ) {
         element.children.forEach(e => {
            if (lastValue.indexOf (e.value) > -1 ) {
              e.checked = true;
              element.hasValue = true;
              if (!hasOpen) {
                element.open = true;
                this.data.current = element;
              }
            }
            if (e.children && e.children.length > 0) {

              e.children.forEach(i => {
                if (lastValue.indexOf (i.value) > -1 ) {
                  i.checked = true;
                  element.hasValue = true;
                  if (hasOpen) {
                    element.open = true;
                    this.data.current = element;
                  }
                }
              });
           }
         });
       }
     });
    } else {
      if ( this.data && this.data.children && this.data.children.length > 0) {
        this.data.children[0].open = true;
        this.data.current = this.data.children[0];
      }

    }
    this.checkBigVaue(this.data);
  }

    // 左边大类点击
    itemLeftClick(it, item) {
      item.children.forEach(element => {
        element.open = false;
      });
      it.open = true;
      item.current = it;
      this.checkBigVaue(item);
    }
    chooseCurrentAll(item) {
      item.current.checked = !item.current.checked;
      if (item.current.checked) {
        item.current.hasValue = true;
        item.current.children.forEach(element => {
          element.checked = true;
          element.children.forEach(e => {
            e.checked = true;

          });
        });
      } else {
        item.current.hasValue = false;
        item.current.children.forEach(element => {
          element.checked = false;
          element.children.forEach(e => {
            e.checked = false;

          });
        });
      }
      this.checkBigVaue(item);
      console.log(item.value);
      // this.formGroup.patchValue({ [item.formKey]: item.value });
    }

    chooseAll(it, item) {
      it.checked = !it.checked;

      if (it.checked) {
        if (item.current) {
          item.current.hasValue = true;
        }

        if (typeof item.value === 'string') {
          item.value = item.value.split(',');
        }
        item.value.push(it.value);
        // 加入大类 清除小类
        it.children.forEach(element => {
          element.checked = true;
          const index = item.value.findIndex(v => (v === element.value));
          if (index > -1) {
            item.value.splice(index, 1);
          }
        });

        // this.checkBigVaue(item);
      } else {
        if (item.current) {
          item.current.hasValue = false;
        }
        it.children.forEach(element => {
          element.checked = false;
          //  清除 大类、小类
          const index = item.value.findIndex(v => (v === element.value) || it.value === v);
          if (index > -1) {
            item.value.splice(index, 1);
          }
        });

      }
      this.checkBigVaue(item);
      console.log(item.value);
      $('#' + it.value).slideToggle("slow");
      // this.formGroup.patchValue({ [item.formKey]: item.value });

    }

    tagClick(tag, it, item) {
      tag.checked = !tag.checked;
      if (typeof item.value === 'string') {
        item.value = item.value.split(',');
        if (item.value.indexOf('') > -1) {
          item.value.splice(item.value.indexOf(''), 1);
        }

      }
      // 没有第三级
      if (tag === it) {
        if (tag.checked) {
          item.current.hasValue = true;

        } else {
          let hasValue = false;
          item.current.children.forEach(element => {
            if (element.checked) {
              hasValue = true;

            }
          });
          item.current.hasValue = hasValue;
        }
      } else {
        let i = 0;
        it.children.forEach(element => {
          element.checked ? ++i : '';
        });
        (i === it.children.length && i > 0) ? it.checked = true : it.checked = false;
        if (it.checked) {
          if (item.current) {
            item.current.hasValue = true;
          }

          // 加入大类 清除小类
          item.value.push(it.value);
          it.children.forEach(element => {

            const index = item.value.findIndex(v => (v === element.value));
            if (index > -1) {
              item.value.splice(index, 1);
            }

          });
        } else {
          it.children.forEach(element => {
            // 先清除
            if (item.current) {
              item.current.hasValue = false;
            }
            if (item.value) {
              const index = item.value.findIndex(v => (v === element.value || v === it.value));
              if (index > -1) {
                item.value.splice(index, 1);
              }
            }


          });

          // 再加入
          it.children.forEach(element => {
            if (element.checked) {
              if (item.current) {
                item.current.hasValue = true;
              }
              item.value.push(element.value);
            }
          });
        }
      }

      this.checkBigVaue(item);
      console.log(item.value);
      // this.formGroup.patchValue({ [item.formKey]: item.value });

    }
    checkBigVaue(item) {
      // 遍历当前二级分类是否全选
      if (item.current) {
        let n = 0;
        item.current.children.forEach(element => {
          element.checked ? ++n : '' ;
        });
        // 全选择 清除 二三级
        if (item.current.children.length === n) {
          item.current.checked = true;
          item.value.push(item.current.value);
          // 清除 二级
          item.current.children.forEach(element => {

            const index = item.value.findIndex(v => (v === element.value));
            if (index > -1) {
              item.value.splice(index, 1);
            }
            // 清除 三级
            this.removeValue(item, element);
          });
        } else {
          item.current.checked = false;
          // 清除一级分类value
          const index = item.value.findIndex(v => (v === item.current.value));
          if (index > -1) {
            item.value.splice(index, 1);
          }
          // 遍历 二 三级
          item.current.children.forEach(element => {
            if (element.checked) {
              item.current.hasValue = true;
              if (item.value.findIndex(v => (v === element.value)) === -1) {
                item.value.push(element.value);
              }

            } else {
              if (item.value.findIndex(v => (v === element.value)) > -1) {
                item.value.splice(item.value.findIndex(v => (v === element.value), 1));
              }
              element.children.forEach(e => {
                if (e.checked) {
                  item.current.hasValue = true;
                  if (item.value.findIndex(v => (v === e.value)) === -1) {

                    item.value.push(e.value);
                  }
                }
              });
            }

          });
        }
      }
    }

    removeValue(item, it) {
      it.children.forEach(e => {
        const index = item.value.findIndex(v => (v === e.value));
        if (index > -1) {
          item.value.splice(index, 1);

        }
      });
    }
    addValue(item, it) {
      it.children.forEach(e => {
        if (e.checked && item.value.findIndex(v => (v === e.value)) === -1) {
          item.value.push(e.value);
        }
      });
    }
    resetClick() {
      this.data.children.forEach(element => {
         element.checked = false;
         element.hasValue = false;
         if (element.children && element.children.length > 0) {
          element.children.forEach(e => {
            e.checked = false;
            if (e.children && e.children.length > 0) {
              e.children.forEach(i => {
                i.checked = false;

              });
            }
          });
        }
      });
    }
    sureClick() {
      // const labelArray: any = [];
      const valueArray: any = [];
      this.data.children.forEach(element => {

        if (element.children && element.children.length > 0 && element.hasValue) {
         element.children.forEach(e => {
            if (e.checked) {
              valueArray.push(e);
            }
            if (e.children && e.children.length > 0) {

            e.children.forEach(i => {
               if (i.checked) {
                valueArray.push(i);

               }

             });
           }
         });
       }
     });

      this.modalController.dismiss(valueArray);

    }
    dismissClick() {
      this.modalController.dismiss();
    }
}
